﻿using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Tools
{
    public partial class Main : Base
    {
        string RootPath = AppDomain.CurrentDomain.BaseDirectory + "soft\\";//文件夹路径

        public Main()
        {
            InitializeComponent();
            MyIcon.Icon = global::Tools.Properties.Resources.main;
            SetDefaultLocation();
            DataBind_info();
            this.Tab1.Tab_Load();
        }

        #region 设置默认位置

        /// <summary>
        /// 默认位置
        /// </summary>
        private new void SetDefaultLocation()
        {
            //this.StartPosition = FormStartPosition.Manual;
            //var obj = GetMaxScreen();
            //this.DesktopLocation = obj.Bounds.Location;

            //if (obj.Bounds.Location.X > 0)//主屏幕
            //{
            //    this.Left = obj.Bounds.Location.X + (obj.Bounds.Width - this.Width) / 2;
            //}
            //else if (obj.Bounds.Location.X == 0)//主屏幕
            //{
            //    this.Left = (obj.Bounds.Width - this.Width) / 2;
            //}
            //else if (obj.Bounds.Location.X < 0)
            //{
            //    this.Left = -((obj.Bounds.Width - this.Width) / 2 + this.Width);
            //}

            //if (obj.Bounds.Location.Y == 0)
            //{
            //    this.Top = (obj.Bounds.Height - this.Height) / 2;
            //}
            //if (obj.Bounds.Location.Y > 0)
            //{
            //    this.Top = (obj.Bounds.Height - this.Height) / 2 + obj.Bounds.Location.Y;
            //}
            //else if (obj.Bounds.Location.Y < 0)
            //{
            //    var top = (obj.Bounds.Height - this.Height) / 2;
            //    this.Top = obj.Bounds.Location.Y - (-top);
            //}


            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);

            var height = Convert.ToInt32(rect.Height * 0.6);
            this.Height = height > 680 ? 680 : height;

            this.Left = rect.Width - 400;
            this.Top = (rect.Height - this.Height) / 2;
        }

        /// <summary>
        /// 获取多屏幕中最大分辨率屏幕
        /// </summary>
        public static System.Windows.Forms.Screen GetMaxScreen()
        {
            var index = 0;
            var items = System.Windows.Forms.Screen.AllScreens;
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i].Bounds.Width > items[i].Bounds.Height)
                {
                    index = i;
                }
            }
            return items[index];
        }
        #endregion

        #region 托盘图标

        private void MyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.TopMost = true;
                this.Show();
                WindowState = FormWindowState.Normal;
                this.Focus();
                this.TopMost = false;
            }
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }
        }

        private void 显示主面板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            WindowState = FormWindowState.Normal;
            this.Focus();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }
        private void 所在目录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            System.Diagnostics.Process.Start(path);
        }
        private void 开机启动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RegistryKey R_local = Registry.CurrentUser;
            RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            R_run.SetValue("Tools", "\"" + Application.ExecutablePath + "\"");
            R_run.Close();
            R_local.Close();
        }

        #endregion

        #region 按钮点击事件

        private void btn_fileMd5_Click(object sender, EventArgs e)
        {
            var obj = new FileMd5();
            obj.Show();
        }
        private void btn_url_Click(object sender, EventArgs e)
        {
            var obj = new UrlCode();
            obj.Show();
        }
        private void btn_md5_Click(object sender, EventArgs e)
        {
            var obj = new Md5();
            obj.Show();
        }
        private void btn_fanyi_Click(object sender, EventArgs e)
        {
            var obj = new FanYi();
            obj.Show();
        }

        private void bt_quse_Click(object sender, EventArgs e)//雅虎取色
        {
            System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath + "\\Soft\\雅虎屏幕取色.exe");
        }
        private void btn_CMD_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.FileName = "powershell.exe";
            p.Start();
        }
        private void btn_chrome_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.baidu.com/");
        }
        private void btn_prot_Click(object sender, EventArgs e)
        {
            var obj = new KillProt();
            obj.Show();
        }
        private void btn_Google_Click(object sender, EventArgs e)
        {
            var obj = new GoogleCalendar();
            obj.Show();
        }
        private void btn_table_Click(object sender, EventArgs e)
        {
            var obj = new TableStruct();
            obj.Show();
        }

        private void btn_Microsoft_Click(object sender, EventArgs e)
        {
            var obj = new MicrosoftCalendar();
            obj.Show();
        }
        #endregion

        #region 绑定快捷图标

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void DataBind_info()
        {
            listView1.Items.Clear();
            imageList1.Images.Clear();

            DirectoryInfo Dir = new DirectoryInfo(RootPath);
            if (!Directory.Exists(RootPath))
            {
                Directory.CreateDirectory(RootPath);
            }
            int j = 0;
            foreach (FileInfo f in Dir.GetFiles("*.*")) //查找文件
            {
                string hz = System.IO.Path.GetExtension(f.ToString());//后去文件后缀名
                if (hz.IndexOf("ini") < 0)
                {
                    var item = new ListViewItem()
                    {
                        Text = f.Name.Substring(0, f.Name.LastIndexOf(".")),
                        ImageIndex = j,
                        Tag = f.Name
                    };

                    //listView1.Items.Add(f.Name, j);
                    listView1.Items.Add(item);

                    var ico = Helper.FileIcon.GetIco(RootPath + f.ToString());
                    imageList1.Images.Add(ico.ToBitmap());
                    ico.Dispose();

                    //imageList1.Images.Add(Icon.ExtractAssociatedIcon(RootPath + f.ToString()).ToBitmap());

                    j++;
                }
            }

            //添加图标
            listView1.LargeImageList = imageList1;
            for (int i = 1; i < listView1.Items.Count; i++)
            {
                listView1.Items[i].ImageIndex = i;
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)//双击打开程序
        {
            try
            {
                String Name = listView1.SelectedItems[0].Tag.ToString();
                Process.Start(RootPath + Name);//打开程序
            }
            catch
            {
                MessageBox.Show("抱歉！出错啦！");
            }
        }
        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                String str = listView1.SelectedItems[0].Text;
                Point p = new Point(e.X, e.Y);
                contextMenuStrip1.Show(listView1, p);
            }
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)//这这判断是否点了空白区，点了空白区进到if里   
            {
                if (e.Button == MouseButtons.Right)
                {
                    Point p = new Point(e.X, e.Y);
                    contextMenuStrip2.Show(listView1, p);
                }
            }
        }
        #endregion

        #region 快捷方式 右键菜单

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String Name = listView1.SelectedItems[0].Tag.ToString();
                Process.Start(RootPath + Name);//打开程序
            }
            catch
            {
                MessageBox.Show("抱歉！出错啦！");
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String Name = listView1.SelectedItems[0].Tag.ToString();
            File.Delete(RootPath + Name);
            //绑定数据
            DataBind_info();
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataBind_info();
        }

        private void 打开所在文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(RootPath);//打开程序
        }

        #endregion

        #region 拖动事件

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();

                #region 获取文件夹下面的文件数量

                DirectoryInfo Dir = new DirectoryInfo(RootPath);
                int count = 1;
                foreach (FileInfo f in Dir.GetFiles("*.*")) //查找文件
                {
                    string hz = System.IO.Path.GetExtension(f.ToString());//后去文件后缀名
                    if (hz.IndexOf("ini") < 0)
                    {
                        count++;
                    }
                }
                #endregion 获取文件夹下面的文件数量

                #region 从新对文件命名

                string Name = System.IO.Path.GetFileName(path);
                if (count < 10)
                {
                    Name = "0" + count.ToString() + "." + Name;
                }
                else if (count < 100)
                {
                    Name = count.ToString() + "." + Name;
                }
                else
                {
                    Name = count.ToString() + "." + Name;
                }
                #endregion

                string pToPath = RootPath + Name;
                File.Copy(path, pToPath, true);

                //绑定数据
                DataBind_info();
            }
            catch
            { }
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        #endregion

        #region 底部菜单

        private void link_regedit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Tools.Helper.Tools.ExecCmd("regedit");
        }
        private void link_services_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Tools.Helper.Tools.ExecCmd("services.msc");
        }

        private void link_ControlPanel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Tools.Helper.Tools.ExecCmd("control.exe");
        }

        private void link_eventvwr_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Tools.Helper.Tools.ExecCmd("eventvwr.msc");

        }
        #endregion

        private void panel_Paint(object sender, PaintEventArgs e)
        {
        }

        private void customButton2_Click(object sender, EventArgs e)
        {
            var obj = new Tasks();
            obj.Show();
        }

        private void customButton3_Click(object sender, EventArgs e)
        {
            var obj = new Other();
            obj.Show();
        }
    }
}
